<template>
  <el-dropdown trigger="click" @command="changeLanguage">
    <i :class="'iconfont icon-zhongyingwen'" class="toolBar-icon"></i>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item
          v-for="item in languageList"
          :key="item.value"
          :command="item.value"
          :disabled="language === item.value"
        >
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { computed } from "vue";
import { useGlobalStore } from "@/stores/modules/global";
import { LanguageType } from "@/stores/interface";
import { useRoute, useRouter } from "vue-router";
import { useTabsStore } from "@/stores/modules/tabs";
import { useUserStore } from "@/stores/modules/user";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const tabsStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();

const i18n = useI18n();
const globalStore = useGlobalStore();
const language = computed(() => globalStore.language);
// const router = useRouter();
// const userStore = useUserStore();
// const tabsStore = useTabsStore();
const languageList = [
  { label: "简体中文", value: "zh" },
  { label: "English", value: "en" }
];

const changeLanguage = (lang: string) => {
  i18n.locale.value = lang;
  globalStore.setGlobalState("language", lang as LanguageType);
  // 2.添加动态路由
  // initDynamicRouter();
  // tabsStore.closeMultipleTab();
  // // router.push("/home/index");
  // router.go(0);
  // window.location.reload();
  tabsStore.setTabs([]);
  keepAliveStore.setKeepAliveName([]);
  initDynamicRouter().then(res => {
    // 3.清空 tabs、keepAlive 数据
    tabsStore.setTabs([]);
    keepAliveStore.setKeepAliveName([]);
    // 4.跳转到首页
    router.push("/home/index");
  });
  // window.location.reload();
};
</script>
